home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivparser.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  9.5 KB  |  461 lines

  1. unit IvParser;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8. {$IFDEF WIN32}
  9.   Windows,
  10. {$ELSE}
  11.   WinTypes, WinProcs,
  12. {$ENDIF}
  13.   SysUtils;
  14.  
  15. type
  16.   TIvStringParser = class(TObject)
  17.   protected
  18.     FPosition: Integer;
  19.     FMaxPosition: Integer;
  20.     FSeparator: Char;
  21.     FValue: String;
  22.     FConvert: Boolean;
  23.  
  24.     procedure SetValue(const value: String);
  25.  
  26.     function GetCurrentValue: String;
  27.  
  28.   public
  29.     constructor Create;
  30.     constructor CreateValue(const value: String; separator: Char);
  31.  
  32.     function Eol: Boolean;
  33.  
  34.     procedure RemoveWhiteSpaces;
  35.  
  36.     function TakeChar: Char;
  37.     function PeekChar: Char;
  38.  
  39.     function GetString: String;
  40.     function GetChar: Char;
  41.     function GetInteger: Longint;
  42.     function GetFloat: Double;
  43.     function GetBoolean: Boolean;
  44.  
  45.     function GetCharDef(defaultValue: Char): Char;
  46.     function GetIntegerDef(defaultValue: Longint): Longint;
  47.     function GetFloatDef(defaultValue: Double): Double;
  48.     function GetBooleanDef(defaultValue: Boolean): Boolean;
  49.  
  50.     class function CodeStr(const str: String): String;
  51.     class function DecodeStr(const str: String): String;
  52.  
  53.     property Position: Integer read FPosition;
  54.     property Separator: Char read FSeparator write FSeparator;
  55.     property CurrentValue: String read GetCurrentValue;
  56.     property Value: String read FValue write SetValue;
  57.     property Convert: Boolean read FConvert write FConvert;
  58.   end;
  59.  
  60. {$IFDEF WIN32}
  61.   TIvAnsiParser = class(TIvStringParser)
  62.   end;
  63. {$ELSE}
  64.   TIvAnsiParser = class(TObject)
  65.   protected
  66.     FPosition: Integer;
  67.     FMaxPosition: Integer;
  68.     FSeparator: Char;
  69.     FValue: PChar;
  70.     FConvert: Boolean;
  71.  
  72.     procedure SetValue(const value: PChar);
  73.  
  74.   public
  75.     constructor Create;
  76.     constructor CreateValue(const value: PChar; separator: Char);
  77.     destructor Destroy; override;
  78.  
  79.     function Eol: Boolean;
  80.  
  81.     function TakeChar: Char;
  82.  
  83.     function GetString: String;
  84.     function GetChar: Char;
  85.     function GetInteger: Longint;
  86.     function GetFloat: Double;
  87.     function GetBoolean: Boolean;
  88.  
  89.     property Position: Integer read FPosition;
  90.     property Separator: Char read FSeparator write FSeparator;
  91.     property Value: PChar read FValue write SetValue;
  92.     property Convert: Boolean read FConvert write FConvert;
  93.   end;
  94. {$ENDIF}
  95.  
  96. implementation
  97.  
  98. constructor TIvStringParser.Create;
  99. begin
  100.   inherited Create;
  101.   FConvert := False;
  102.   FPosition := 0;
  103.   FSeparator := #9;
  104.   SetValue('');
  105. end;
  106.  
  107. constructor TIvStringParser.CreateValue(const value: String; separator: Char);
  108. begin
  109.   inherited Create;
  110.   FConvert := False;
  111.   FPosition := 0;
  112.   FSeparator := separator;
  113.   SetValue(value);
  114. end;
  115.  
  116. procedure TIvStringParser.SetValue(const value: String);
  117. begin
  118.   FValue := value;
  119.   FMaxPosition := Length(FValue) + 1;
  120.   FPosition := 1;
  121. end;
  122.  
  123. function TIvStringParser.Eol: Boolean;
  124. begin
  125.   Result := FPosition >= FMaxPosition;
  126. end;
  127.  
  128. procedure TIvStringParser.RemoveWhiteSpaces;
  129. begin
  130.   while (not Eol) and ((FValue[FPosition] = ' ') or (FValue[FPosition] = #9)) do
  131.     Inc(FPosition);
  132. end;
  133.  
  134. function TIvStringParser.TakeChar: Char;
  135. begin
  136.   if Eol then
  137.     Result := Chr(0)
  138.   else
  139.   begin
  140.     Result := FValue[FPosition];
  141.     Inc(FPosition);
  142.   end;
  143. end;
  144.  
  145. function TIvStringParser.PeekChar: Char;
  146. begin
  147.   if Eol then
  148.     Result := Chr(0)
  149.   else
  150.     Result := FValue[FPosition];
  151. end;
  152.  
  153. class function TIvStringParser.CodeStr(const str: String): String;
  154. var
  155.   c: Char;
  156.   i: Integer;
  157. begin
  158.   Result := '';
  159.   for i := 1 to Length(str) do
  160.   begin
  161.     c := str[i];
  162.     case c of
  163.       #9: Result := Result + '#T';
  164.       #10: Result := Result + '#C';
  165.       #13: Result := Result + '#L';
  166.     else
  167.       Result := Result + c;
  168.     end;
  169.   end;
  170. end;
  171.  
  172. class function TIvStringParser.DecodeStr(const str: String): String;
  173. var
  174.   c: Char;
  175.   len, src, dest: Integer;
  176. begin
  177. {$IFDEF WIN32}
  178.   len := Length(str);
  179.   SetLength(Result, len);
  180.   dest := 1;
  181.   src := 1;
  182.   while src <= len do
  183.   begin
  184.     c := str[src];
  185.     if c = '#' then
  186.     begin
  187.       Inc(src);
  188.       if src <= len then
  189.       begin
  190.         c := str[src];
  191.         case c of
  192.           '#': c := '#';
  193.           'T': c := #9;
  194.           'L': c := #13;
  195.           'C': c := #10;
  196.         else
  197.           Result[dest] := '#';
  198.           Inc(dest);
  199.         end;
  200.       end;
  201.     end;
  202.  
  203.     Result[dest] := c;
  204.     Inc(src);
  205.     Inc(dest);
  206.   end;
  207.  
  208.   { Sets the string length to actual length }
  209.  
  210.   SetLength(Result, dest - 1);
  211. {$ELSE}
  212.   Result := '';
  213.   len := Length(str);
  214.   src := 1;
  215.   while src <= len do
  216.   begin
  217.     c := str[src];
  218.     if c = '#' then
  219.     begin
  220.       Inc(src);
  221.       if src <= len then
  222.       begin
  223.         c := str[src];
  224.         case c of
  225.           '#': c := '#';
  226.           'T': c := #9;
  227.           'L': c := #13;
  228.           'C': c := #10;
  229.         else
  230.           Result := Result + '#';
  231.         end;
  232.       end;
  233.     end;
  234.  
  235.     Result := Result + c;
  236.     Inc(src);
  237.   end;
  238. {$ENDIF}
  239. end;
  240.  
  241. function TIvStringParser.GetCurrentValue: String;
  242. var
  243.   start, pos: Integer;
  244. begin
  245.   if FValue = '' then
  246.     Result := ''
  247.   else
  248.   begin
  249.     pos := FPosition;
  250.     start := FPosition;
  251.     while (pos < FMaxPosition) and (FValue[pos] <> FSeparator) do
  252.       Inc(pos);
  253.  
  254.     if pos = start then
  255.       Result := ''
  256.     else
  257.       Result := Copy(FValue, start, pos - start);
  258.   end;
  259. end;
  260.  
  261. function TIvStringParser.GetString: String;
  262. var
  263.   start: Integer;
  264. begin
  265.   if FValue = '' then
  266.     Result := ''
  267.   else
  268.   begin
  269.     start := FPosition;
  270.     while (FPosition < FMaxPosition) and (FValue[FPosition] <> FSeparator) do
  271.       Inc(FPosition);
  272.  
  273.     if FPosition = start then
  274.       Result := ''
  275.     else
  276.       Result := Copy(FValue, start, FPosition - start);
  277.     Inc(FPosition);
  278.  
  279.     if FConvert and (Pos('#', Result) > 0) then
  280.       Result := DecodeStr(Result);
  281.   end;
  282. end;
  283.  
  284. function TIvStringParser.GetChar: Char;
  285. begin
  286.   Result := GetString[1];
  287. end;
  288.  
  289. function TIvStringParser.GetCharDef(defaultValue: Char): Char;
  290. begin
  291.   if Eol then
  292.     Result := defaultValue
  293.   else
  294.     Result := GetChar;
  295. end;
  296.  
  297. function TIvStringParser.GetInteger: Longint;
  298. begin
  299.   Result := StrToInt(GetString);
  300. end;
  301.  
  302. function TIvStringParser.GetIntegerDef(defaultValue: Longint): Longint;
  303. begin
  304.   Result := StrToIntDef(GetString, defaultValue);
  305. end;
  306.  
  307. function TIvStringParser.GetFloat: Double;
  308. var
  309.   oldDecimalSeparator: Char;
  310. begin
  311.   oldDecimalSeparator := DecimalSeparator;
  312.   DecimalSeparator := '.';
  313.   try
  314.     Result := StrToFloat(GetString);
  315.   finally
  316.     DecimalSeparator := oldDecimalSeparator;
  317.   end;
  318. end;
  319.  
  320. function TIvStringParser.GetFloatDef(defaultValue: Double): Double;
  321. begin
  322.   if Eol then
  323.     Result := defaultValue
  324.   else
  325.     Result := GetFloat;
  326. end;
  327.  
  328. function TIvStringParser.GetBoolean: Boolean;
  329. var
  330.   str: String;
  331. begin
  332.   str := GetString;
  333.   if (str = '0') or (CompareText(str, 'false') = 0) or (CompareText(str, 'no') = 0) then
  334.     Result := False
  335.   else if (str = '1') or (CompareText(str, 'true') = 0) or (CompareText(str, 'yes') = 0) then
  336.     Result := True
  337.   else
  338.     raise Exception.Create(str + ' is not a boolean value');
  339. end;
  340.  
  341. function TIvStringParser.GetBooleanDef(defaultValue: Boolean): Boolean;
  342. begin
  343.   if Eol then
  344.     Result := defaultValue
  345.   else
  346.     Result := GetBoolean;
  347. end;
  348.  
  349.  
  350. { TIvAnsiParser }
  351.  
  352. {$IFNDEF WIN32}
  353. constructor TIvAnsiParser.Create;
  354. begin
  355.   inherited Create;
  356.   FConvert := False;
  357.   FPosition := 0;
  358.   FSeparator := #9;
  359.   SetValue(nil);
  360. end;
  361.  
  362. constructor TIvAnsiParser.CreateValue(const value: PChar; separator: Char);
  363. begin
  364.   inherited Create;
  365.   FConvert := False;
  366.   FPosition := 0;
  367.   FSeparator := separator;
  368.   SetValue(value);
  369. end;
  370.  
  371. destructor TIvAnsiParser.Destroy;
  372. begin
  373.   if FValue <> nil then
  374.     StrDispose(FValue);
  375.   inherited Destroy;
  376. end;
  377.  
  378. procedure TIvAnsiParser.SetValue(const value: PChar);
  379. begin
  380.   { Frees the previous value }
  381.  
  382.   if FValue <> nil then
  383.     StrDispose(FValue);
  384.  
  385.   { Sets the new value }
  386.  
  387.   FValue := value;
  388.   if FValue = nil then
  389.     FMaxPosition := -1
  390.   else
  391.     FMaxPosition := StrLen(FValue);
  392.   FPosition := 0;
  393. end;
  394.  
  395. function TIvAnsiParser.Eol: Boolean;
  396. begin
  397.   Result := FPosition >= FMaxPosition;
  398. end;
  399.  
  400. function TIvAnsiParser.GetString: String;
  401. var
  402.   start: Integer;
  403.   i: Integer;
  404. begin
  405.   if StrLen(FValue) = 0 then
  406.     Result := ''
  407.   else
  408.   begin
  409.     start := FPosition;
  410.     while (FPosition < FMaxPosition) and (FValue[FPosition] <> FSeparator) do
  411.       Inc(FPosition);
  412.  
  413.     if FPosition = start then
  414.       Result := ''
  415.     else
  416.     begin
  417.       Result := '';
  418.       for i := start to FPosition - 1 do
  419.         Result := Result + FValue[i];
  420.     end;
  421.     Inc(FPosition);
  422.   end;
  423. end;
  424.  
  425. function TIvAnsiParser.TakeChar: Char;
  426. begin
  427.   Result := FValue[FPosition];
  428.   Inc(FPosition);
  429. end;
  430.  
  431. function TIvAnsiParser.GetChar: Char;
  432. begin
  433.   Result := GetString[1];
  434. end;
  435.  
  436. function TIvAnsiParser.GetInteger: Longint;
  437. begin
  438.   Result := StrToInt(GetString);
  439. end;
  440.  
  441. function TIvAnsiParser.GetFloat: Double;
  442. begin
  443.   Result := StrToFloat(GetString);
  444. end;
  445.  
  446. function TIvAnsiParser.GetBoolean: Boolean;
  447. var
  448.   str: String;
  449. begin
  450.   str := GetString;
  451.   if (str = '0') or (CompareText(str, 'false') = 0) or (CompareText(str, 'no') = 0) then
  452.     Result := False
  453.   else if (str = '1') or (CompareText(str, 'true') = 0) or (CompareText(str, 'yes') = 0) then
  454.     Result := True
  455.   else
  456.     raise Exception.Create(str + ' is not a boolean value');
  457. end;
  458. {$ENDIF}
  459.  
  460. end.
  461.